package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.annotation.OperationLog;
import cn.zjutleo.village_server.annotation.RequestLimit;
import cn.zjutleo.village_server.annotation.RequiresLogin;
import cn.zjutleo.village_server.dto.AdminLoginDto;
import cn.zjutleo.village_server.dto.PhoneBindDto;
import cn.zjutleo.village_server.dto.WechatLoginDto;
import cn.zjutleo.village_server.exception.apiException.AuthorizationException;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.exception.apiException.smsException.MessageCheckException;
import cn.zjutleo.village_server.service.AuthService;
import cn.zjutleo.village_server.util.ThreadLocalUtil;
import cn.zjutleo.village_server.vo.LoginInfoVo;
import cn.zjutleo.village_server.vo.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author : Qin Zhenghan
 * @date : Created in 2021/8/15
 * @description: 鉴权授权控制类
 */
@Api(tags = "鉴权授权控制类")
@RestController
public class AuthController {
    @Resource
    private AuthService authService;

    /**
     * 模块名(日志输出)
     */
    protected static final String MODULE_NAME = "登录注册模块";

    /**
     * 微信登录
     *
     * @param wechatLoginDto 微信登录DTO
     * @return loginInfoVo 登录用户信息
     * @throws Exception 相关异常类
     */
    @ApiOperation("微信登录")
    @OperationLog(module = MODULE_NAME, method = "微信登录", ignoreParameters = true)
    @RequestLimit(amount = 5, time = 180)
    @PostMapping("/login/wechat")
    public ResponseBean<LoginInfoVo> loginByWechat(@RequestBody @Validated WechatLoginDto wechatLoginDto) throws Exception {
        LoginInfoVo loginInfoVo = authService.loginByWechat(wechatLoginDto);
        return new ResponseBean<>(200, "succ", loginInfoVo);
    }

    /**
     * 绑定手机号
     *
     * @param phoneBindDto 绑定信息实体
     */
    @ApiOperation("绑定手机号")
    @OperationLog(module = MODULE_NAME, method = "绑定手机号")
    @RequiresLogin(requireSignNotice = false)
    @RequestLimit(amount = 1)
    @PostMapping("/bind/phone")
    public ResponseBean<Object> bindPhone(@RequestBody @Validated PhoneBindDto phoneBindDto) throws MessageCheckException, AuthorizationException, SelectException {
        Integer userId = ThreadLocalUtil.getCurrentUser();
        authService.bindPhone(phoneBindDto, userId);
        return new ResponseBean<>(200, "succ", null);
    }

    /**
     * 管理员手机密码登录
     *
     * @param adminLoginDto 管理员登录DTO
     * @return 用户部分信息
     * @throws Exception 鉴权异常
     */
    @ApiOperation("管理员手机密码登录")
    @OperationLog(module = MODULE_NAME, method = "用户登录", ignoreParameters = true)
    @RequestLimit(amount = 5, time = 180)
    @PostMapping("/login/admin")
    public ResponseBean<LoginInfoVo> loginByPhone(@RequestBody @Validated AdminLoginDto adminLoginDto) throws Exception {
        LoginInfoVo loginInfoVo = authService.loginByPhoneAndPassword(adminLoginDto);
        return new ResponseBean<>(200, "登录成功", loginInfoVo);
    }

    /**
     * 验证用户是否绑定手机号
     *
     * @return 是否绑定手机号
     */
    @ApiOperation("检验用户是否绑定手机号")
    @OperationLog(module = MODULE_NAME, method = "检验用户是否绑定手机号", ignoreParameters = true)
    @RequiresLogin()
    @PostMapping("/check/phoneBind")
    public ResponseBean<Boolean> checkPhoneBind() {
        Integer userId = ThreadLocalUtil.getCurrentUser();
        return new ResponseBean<>(authService.checkPhoneBind(userId));
    }
}
